﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using QuanLiThuVien.Source;

namespace QuanLiThuVien
{
    /// <summary>
    /// Interaction logic for NhapSach.xaml
    /// </summary>
    public partial class NhapSach : Window
    {
        private InsertInfo m_insertInfo = new InsertInfo();
        private UpdateInfo m_updateInfo = new UpdateInfo();
        private DeleteInfo m_deleteInfo = new DeleteInfo();
        private ShowInfo m_showInfo = new ShowInfo();
        private XuLyThem m_XuLyKhac = new XuLyThem();

        private THUVIEN.Service1 serv = new THUVIEN.Service1();
        private bool m_bSelectedTheLoai = false;
        private bool m_bSelectedTenSach = false;
        private string m_sMaPhieuNhap;

        public NhapSach()
        {
            InitializeComponent();
            addCmbTacGia(cmbTacGia_NS);
            addCmbTenDauSach(cmbTenSach_NS);
            addCmbTheLoai(cmbTheLoai_NS);
            addCmbNamXuatBan(cmbNamXuatBan_NS);
            dtmNgayNhap_NS.Text = DateTime.Now.ToString();
            m_sMaPhieuNhap = m_insertInfo.InsertPhieuNhap(DateTime.Parse(dtmNgayNhap_NS.SelectedDate.ToString()), 0);
        }

        #region Xu ly cac combobox
        public void addCmbTheLoai(ComboBox m_cmbTheLoai)
        {
            for (int i = 0; i < serv.ListTenTheLoai().Length; i++)
                m_cmbTheLoai.Items.Add(serv.ListTenTheLoai()[i]);
        }

        public void addCmbTenDauSach(ComboBox m_cmbTenDauSach)
        {
            for (int i = 0; i < serv.ListTenDauSach().Length; i++)
                m_cmbTenDauSach.Items.Add(serv.ListTenDauSach()[i]);
        }

        public void addCmbTacGia(ComboBox m_cmbTacGia)
        {
            for (int i = 0; i < serv.ListTenTacGia().Length; i++)
                m_cmbTacGia.Items.Add(serv.ListTenTacGia()[i]);
        }

        public void addCmbNamXuatBan(ComboBox m_cmbNamXuatBan)
        {
            int m_iNamDauTien = DateTime.Now.Year - serv.SelectGiaTriThamSo("KhoangCachNamXB");
            for (int i = m_iNamDauTien; i <= DateTime.Now.Year; i++)
                m_cmbNamXuatBan.Items.Add(i);
        }

        private void addCmbTenDauSach_selectedTheLoai(ComboBox m_cmbTenDauSach)
        {
            m_cmbTenDauSach.Items.Clear();
            for (int i = 0; i < serv.SelectTenDauSach_DauSach(cmbTheLoai_NS.SelectedItem.ToString()).Length; i++)
                m_cmbTenDauSach.Items.Add(serv.SelectTenDauSach_DauSach(cmbTheLoai_NS.SelectedItem.ToString())[i]);
        }

        private void addCmbTheLoai_selectedTenDauSach(ComboBox m_cmbTheLoai)
        {
            m_cmbTheLoai.Items.Clear();
            for (int i = 0; i < serv.SelectTenTheLoai_DauSach(cmbTenSach_NS.SelectedItem.ToString()).Length; i++)
                m_cmbTheLoai.Items.Add(serv.SelectTenTheLoai_DauSach(cmbTenSach_NS.SelectedItem.ToString())[i]);
        }

        private void addCmbTacGia_selectedDauSach(ComboBox m_cmbTacGia)
        {
            m_cmbTacGia.Items.Clear();
            if (m_bSelectedTheLoai == false || m_bSelectedTenSach == false)
            {
                string m_sMaDauSach = serv.SelectMaDauSach(cmbTenSach_NS.SelectedItem.ToString(), serv.SelectMaTheLoai(cmbTheLoai_NS.SelectedItem.ToString()));
                for (int i = 0; i < serv.SelectTenTacGia_CTTG(m_sMaDauSach).Length; i++)
                    m_cmbTacGia.Items.Add(serv.SelectTenTacGia_CTTG(m_sMaDauSach)[i]);
            }
        }

        private void cmbTheLoai_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (m_bSelectedTenSach == false)
            {
                addCmbTenDauSach_selectedTheLoai(cmbTenSach_NS);
                m_bSelectedTheLoai = true;
            }
            else
            {
                if (m_bSelectedTheLoai == false)
                {
                    m_bSelectedTheLoai = true;
                }
                else
                {
                    cmbTenSach_NS.SelectedItem = null;
                    cmbTacGia_NS.SelectedItem = null;
                    m_bSelectedTenSach = false;
                }
            }
        }

        private void cmbTenSach_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (m_bSelectedTheLoai == false)
            {
                addCmbTheLoai_selectedTenDauSach(cmbTheLoai_NS);
                m_bSelectedTenSach = true;
            }
            else
            {
                if (m_bSelectedTenSach == false)
                {
                    m_bSelectedTenSach = true;
                }
                else
                {
                    cmbTheLoai_NS.SelectedItem = null;
                    cmbTacGia_NS.SelectedItem = null;
                    m_bSelectedTheLoai = false;
                }
            }
        }
        #endregion

        private void ClearIndex() 
        {
            int m_iSoLuong = 0;
            int m_iTriGia = 0;
            int m_iThanhTien = 0;
            cmbTacGia_NS.SelectedItem = null;
            cmbTenSach_NS.SelectedItem = null;
            cmbTheLoai_NS.SelectedItem = null;
            txtNhaXuatBan_NS.Text = "";
            cmbNamXuatBan_NS.SelectedItem = null;
            txtDonGia_NS.Text = m_iTriGia.ToString();
            txtSoLuong_NS.Text = m_iSoLuong.ToString();
            txtThanhTien_NS.Text = m_iThanhTien.ToString();
            dtmNgayNhap_NS.Text = DateTime.Now.ToString();
            bntTiepTucNhap_NS.IsEnabled = false;
        }

        private bool KiemTraNhap()
        {
            if (cmbTenSach_NS.SelectedItem == null
                || cmbTheLoai_NS.SelectedItem == null
                || cmbNamXuatBan_NS.SelectedItem == null
                || txtNhaXuatBan_NS.Text == ""
                || txtSoLuong_NS.Text == ""
                || txtDonGia_NS.Text == "")
                return true;
            else return false;
        }

        private void bntThemCTTacGia_Click(object sender, RoutedEventArgs e)
        {
            string m_sMaTheLoai = serv.SelectMaTheLoai(cmbTheLoai_NS.SelectedItem.ToString());
            string m_sMaDauSach = serv.SelectMaDauSach(cmbTenSach_NS.SelectedItem.ToString(), m_sMaTheLoai);
            m_insertInfo.InsertCTTacGia(serv.SelectMaTacGia(cmbTacGia_NS.SelectedItem.ToString()),m_sMaDauSach);
        }

        private void bntThoat_NS_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnRefeshCombobox_NS_Click(object sender, RoutedEventArgs e)
        {
            cmbTenSach_NS.Items.Clear();
            cmbTheLoai_NS.Items.Clear();
            addCmbTheLoai(cmbTheLoai_NS);
            addCmbTenDauSach(cmbTenSach_NS);
        }

        private void bntThemDauSach_NS_Click(object sender, RoutedEventArgs e)
        {
            DauSach m_fDauSach = new DauSach();
            m_fDauSach.Show();
            cmbTacGia_NS.IsEnabled = true;
            bntRefeshTacGia.IsEnabled = true;
            bntThemTacGia.IsEnabled = true;
            bntThemCTTacGia.IsEnabled = true;
        }

        private void cmbTacGia_NS_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            bntThemCTTacGia.IsEnabled = true;
        }
        
        private void txtTriGia_NS_TextChanged(object sender, TextChangedEventArgs e)
        {
            m_XuLyKhac.IsNumber(txtDonGia_NS);
            int m_iThanhTien = 0;
            if (txtSoLuong_NS.Text == "")
                txtThanhTien_NS.Text = m_iThanhTien.ToString();
            else
            {
                m_iThanhTien = Int32.Parse(txtSoLuong_NS.Text) * Int32.Parse(txtDonGia_NS.Text);
                txtThanhTien_NS.Text = m_iThanhTien.ToString();
            }
        }

        private void txtSoLuong_NS_TextChanged(object sender, TextChangedEventArgs e)
        {
            m_XuLyKhac.IsNumber(txtSoLuong_NS);
            int m_iThanhTien = 0;
            if (txtDonGia_NS.Text == "")
                txtThanhTien_NS.Text = m_iThanhTien.ToString();
            else
            {
                m_iThanhTien = Int32.Parse(txtSoLuong_NS.Text) * Int32.Parse(txtDonGia_NS.Text);
                txtThanhTien_NS.Text = m_iThanhTien.ToString();
            }
        }

        private void txtThanhTien_NS_TextChanged(object sender, TextChangedEventArgs e)
        {
            m_XuLyKhac.IsNumber(txtThanhTien_NS);
        }

        private void bntChapNhan_NS_Click(object sender, RoutedEventArgs e)
        {
            if (KiemTraNhap())
                MessageBox.Show("Phải nhập đầy đủ thông tin", "Thông báo");
            else
            {
                if (Int32.Parse(cmbNamXuatBan_NS.SelectedItem.ToString()) + serv.SelectGiaTriThamSo("KhoangCachNamXB") < DateTime.Now.Year)
                {
                    MessageBox.Show("Không nhập sách xuất bản quá cũ", "Thông báo");
                    ClearIndex();
                }
                else
                {
                    string m_sMaTheLoai = serv.SelectMaTheLoai(cmbTheLoai_NS.SelectedItem.ToString());
                    string m_sMaDauSach = serv.SelectMaDauSach(cmbTenSach_NS.SelectedItem.ToString(), m_sMaTheLoai);
                    string m_sMaSach = serv.SelectMaSach(m_sMaDauSach, Int32.Parse(cmbNamXuatBan_NS.SelectedItem.ToString()), txtNhaXuatBan_NS.Text, Int32.Parse(txtDonGia_NS.Text));
                    if (m_sMaSach == null)
                    {
                        m_insertInfo.InsertSach(m_sMaDauSach, Int32.Parse(cmbNamXuatBan_NS.SelectedItem.ToString()), txtNhaXuatBan_NS.Text, Int32.Parse(txtSoLuong_NS.Text), Int32.Parse(txtDonGia_NS.Text));
                        m_sMaSach = serv.SelectMaSach(m_sMaDauSach, Int32.Parse(cmbNamXuatBan_NS.SelectedItem.ToString()), txtNhaXuatBan_NS.Text, Int32.Parse(txtDonGia_NS.Text));
                        int m_iThanhTien = Int32.Parse(txtSoLuong_NS.Text) * Int32.Parse(txtDonGia_NS.Text);
                        m_insertInfo.InsertCTPhieuNhap(m_sMaPhieuNhap, m_sMaSach, Int32.Parse(txtSoLuong_NS.Text), m_iThanhTien);

                        for (int i = 0; i < Int32.Parse(txtSoLuong_NS.Text); i++)
                            m_insertInfo.InsertCuonSach(m_sMaSach, 0);

                        int m_iTongTienTruoc = serv.SelectTongTien_PhieuNhap(m_sMaPhieuNhap);
                        m_updateInfo.UpdateTongTien_PhieuNhap(m_sMaPhieuNhap, m_iTongTienTruoc + m_iThanhTien);
                        bntTiepTucNhap_NS.IsEnabled = true;
                    }
                    else
                    {
                        int m_iThanhTien = Int32.Parse(txtSoLuong_NS.Text) * Int32.Parse(txtDonGia_NS.Text);
                        m_insertInfo.InsertCTPhieuNhap(m_sMaPhieuNhap, m_sMaSach, Int32.Parse(txtSoLuong_NS.Text), m_iThanhTien);

                        for (int i = 0; i < Int32.Parse(txtSoLuong_NS.Text); i++)
                            m_insertInfo.InsertCuonSach(m_sMaSach, 0);

                        int m_iTongTienTruoc = serv.SelectTongTien_PhieuNhap(m_sMaPhieuNhap);
                        m_updateInfo.UpdateTongTien_PhieuNhap(m_sMaPhieuNhap, m_iTongTienTruoc + m_iThanhTien);

                        int m_iSoLuongSachHienTai = serv.SelectSoLuong_Sach(m_sMaSach);
                        m_updateInfo.UpdateSoLuong_Sach(m_sMaSach, m_iSoLuongSachHienTai + Int32.Parse(txtSoLuong_NS.Text));

                        bntTiepTucNhap_NS.IsEnabled = true;
                    }
                }
            }
        }

        private void bntTiepTucNhap_NS_Click(object sender, RoutedEventArgs e)
        {
            ClearIndex();
        }

        private void bntThemTacGia_Click(object sender, RoutedEventArgs e)
        {
            TacGia m_fTacGia = new TacGia();
            m_fTacGia.Show();
        }

        private void bntRefeshTacGia_Click(object sender, RoutedEventArgs e)
        {
            cmbTacGia_NS.Items.Clear();
            addCmbTacGia(cmbTacGia_NS);
        }

        private void txtNhaXuatBan_NS_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                bntChapNhan_NS_Click(sender, e);
        }

        private void txtNamXuatBan_NS_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                bntChapNhan_NS_Click(sender, e);
        }

        private void txtDonGia_NS_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                bntChapNhan_NS_Click(sender, e);
        }

        private void txtSoLuong_NS_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
                bntChapNhan_NS_Click(sender, e);
        }

        private void cmbNamXuatBan_NS_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }


        //private void cmbTacGia_SelectionChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    if (m_bSelectedTheLoai == true || m_bSelectedTenSach == true)
        //    {
        //        addCmbTacGia_selectedDauSach(cmbTacGia);
        //        m_bSelectedTacGia = true;
        //    }
        //}

    }
}
